"use strict";
// 装饰器
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
// 1.装饰器的基本使用
const moveDecorator = (target) => {
    // 拓展类方法
    target.prototype.getPosition = function () {
        console.log('getPosition' + { x: 12, y: 56 });
        return { x: 12, y: 56 };
    };
};
let Tank = class Tank {
};
Tank = __decorate([
    moveDecorator
], Tank);
;
let tank1 = new Tank();
tank1.getPosition();
tank1.getPosition();
// 2.使用装饰器统一增加方法（代替类的继承方式）
let messageDecorator = (target) => {
    // 增加message方法
    target.prototype.message = (val) => {
        console.log('发布信息：' + val);
    };
};
let Player = class Player {
    login() {
        console.log('欢迎你---player');
        this.message('message1');
    }
};
Player = __decorate([
    messageDecorator
], Player);
(new Player).login();
let Test = class Test {
    login() {
        console.log('欢迎你---Test');
        this.message('message---test');
    }
};
Test = __decorate([
    messageDecorator
], Test);
;
(new Test).login();
// 3.类装饰器工厂
let musicDecoratorFactory = function (type) {
    switch (type) {
        case 'tank':
            return (target) => {
                target.prototype.palyMusic = function () {
                    console.log('播放战争音乐');
                };
            };
            break;
        default:
            return (target) => {
                target.prototype.palyMusic = function () {
                    console.log('播放欢快的音乐');
                };
            };
            break;
    }
};
let Tank2 = class Tank2 {
};
Tank2 = __decorate([
    musicDecoratorFactory('tank')
], Tank2);
;
let Player2 = class Player2 {
};
Player2 = __decorate([
    musicDecoratorFactory('player')
], Player2);
(new Tank2).palyMusic();
(new Player2).palyMusic();
// 4.方法装饰器
const FnDecorator = (target, propertyKey, descriptor) => {
    const method = descriptor.value;
    descriptor.value = function () {
        console.log('hello word---change');
    };
};
class TestPro {
    show(str) {
        return 'hello world';
    }
}
__decorate([
    FnDecorator,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", String)
], TestPro.prototype, "show", null);
(new TestPro).show('test');
// 实现代码高亮
const methodDecorator = (target, propertyKey, descriptor) => {
    const method = descriptor.value;
    console.log(JSON.stringify(descriptor.value));
    descriptor.value = () => {
        return `<div style="color:red;">${method()}</div>`;
    };
};
class HeightColor {
    showTxt(str) {
        // return str;
        return 'cnsdsdb';
    }
}
__decorate([
    methodDecorator,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", String)
], HeightColor.prototype, "showTxt", null);
;
let txt = (new HeightColor).showTxt('黑来咯biddy');
console.log(txt);
// 5.通过函数在装饰器工厂实现延时
const delayFn = (time) => {
    return (target, proprertyKey, descripter) => {
        let method = descripter.value;
        descripter.value = () => {
            setTimeout(function () {
                method();
            }, time || 500);
        };
    };
};
class DelayTime {
    runing() {
        console.log('延时后执行');
    }
}
__decorate([
    delayFn(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], DelayTime.prototype, "runing", null);
let instance1 = new DelayTime();
instance1.runing();
//  6.通过装饰器实现一个自定义错误打印
const ErrorDecorator = (title = '自定义错误文案', fontSize = 14) => {
    return (target, propertyKey, descriptor) => {
        let method = descriptor.value;
        descriptor.value = () => {
            try {
                method();
            }
            catch (err) {
                console.log(`%c${err}-${title}`, `color: red;font-size:${fontSize}px`);
            }
        };
    };
};
class ErrorTest {
    getErr() {
        throw Error('您查找的用户不存在');
    }
}
__decorate([
    ErrorDecorator(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], ErrorTest.prototype, "getErr", null);
// 7.属性装饰器
const propertyDecoratorFn = (target, propertyKey) => {
    Object.defineProperty(target, propertyKey, {
        get() {
            console.log('get----', propertyKey);
        },
        set(val) {
            console.log('set----', propertyKey, val);
        }
    });
};
// const propertyDecoratorFn: PropertyDecorator = (...ags: any[]) => {
//   console.log(ags);
// }
class PropertyCls {
}
__decorate([
    propertyDecoratorFn,
    __metadata("design:type", Object)
], PropertyCls.prototype, "name", void 0);
let instance4 = new PropertyCls;
instance4.name = 'zhoumingle';
instance4.name;
// 8.参数装饰器
const parameDecoratorFn = (target, propertyKey, parameterIndex) => {
    console.log('参数装饰器:', target, propertyKey, parameterIndex);
};
class ParameCls {
    get(name) {
        console.log('hello word-----!!!');
    }
}
__decorate([
    __param(0, parameDecoratorFn),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", void 0)
], ParameCls.prototype, "get", null);
let instance2 = new ParameCls;
instance2.get('123');
